package com.imooc.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imooc.thrift.user.UserInfo;
import com.imooc.thrift.user.UserService;
import com.imooc.user.entity.User;
import com.imooc.user.mapper.UserMapper;
import org.apache.thrift.TException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author xiaoxiaokui
 * @date 2020/2/22
 */
@Service
public class UserServiceImpl implements UserService.Iface {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserInfo getUserById(int id) throws TException {
        return this.getUserInfoByUser(userMapper.selectById(id));
    }

    @Override
    public UserInfo getUserByName(String username) throws TException {
        User user = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUsername, username));
        return this.getUserInfoByUser(user);
    }

    @Override
    public void registerUser(UserInfo userInfo) throws TException {
        User user = new User();
        BeanUtils.copyProperties(userInfo, user);
        userMapper.insert(user);
    }

    /**
     * @Description: user 转换成 userInfo
     * @Param: [user]
     * @return: com.imooc.thrift.user.UserInfo
     * @Author: xiaoxiaokui
     * @Date: 2020/3/3
     */
    private UserInfo getUserInfoByUser(User user) {
        if (null == user) {
            return null;
        }
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(userInfo, user);
        return userInfo;
    }
}
